package com.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.entity.NotificationEntity;

@Repository
public class NotificationDaoImpl extends GenericDaoImpl<NotificationEntity> implements NotificationDao {

	public NotificationEntity getNotificationEntityByIssueId(int id) {
		Query query = sessionFactory.getCurrentSession().createQuery("from NotificationEntity where issue.idIssue=:id");
		query.setParameter("id", id);
		return (NotificationEntity) query.uniqueResult();
	}

	public void saveOrUpdateNotificationEntity(NotificationEntity entity) {
		Query query = sessionFactory.getCurrentSession().createQuery("from NotificationEntity where issue=:issue and user=:user");
		query.setParameter("issue", entity.getIssue());
		query.setParameter("user", entity.getUser());
		NotificationEntity notification = (NotificationEntity) query.uniqueResult();
		if (notification != null) {
			notification.setLastNotificationDate(entity.getLastNotificationDate());
			notification.setLastNotificationResult(entity.getLastNotificationResult());
		} else {
			notification = new NotificationEntity();
			notification.setIssue(entity.getIssue());
			notification.setLastNotificationDate(entity.getLastNotificationDate());
			notification.setLastNotificationResult(entity.getLastNotificationResult());
			notification.setUser(entity.getUser());
		}
		sessionFactory.getCurrentSession().saveOrUpdate(notification);
	}

}
